package com.example.ymm.modules1.service.impl.ms;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.bo.YmUserDetails;
import com.example.ymm.model.TableList;
import com.example.ymm.model.UserLogin;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.ms.MsOrderItemView;
import com.example.ymm.modules1.entity.my.sys.iot.IotTag;
import com.example.ymm.modules1.mapper.ms.MsOrderItemViewMapper;
import com.example.ymm.modules1.query.ms.MsOrderItemViewQueryCriteria;
import com.example.ymm.modules1.query.sys.iot.IotTagQueryCriteria;
import com.example.ymm.modules1.service.dd.iot.IotHttpUtil;
import com.example.ymm.modules1.service.dto.ms.ListenerOutOrderItemDto;
import com.example.ymm.modules1.service.dto.ms.ListenerStockItemDto;
import com.example.ymm.modules1.service.ms.MsOrderItemViewService;
import com.example.ymm.modules1.service.sys.iot.IotTagService;
import com.example.ymm.util.JwtTokenUtil;
import com.example.ymm.util.RedisKeyManager;
import com.example.ymm.util.RedisUtil;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class MsOrderItemViewServiceImpl extends BaseServiceImpl<MsOrderItemViewMapper, MsOrderItemView> implements MsOrderItemViewService {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    IotTagService iotTagService;
    @Autowired
    IotHttpUtil iotHttpUtil;

    @Override
    public TableList<MsOrderItemView> list(MsOrderItemViewQueryCriteria criteria, YmPageDto pageDto) {
//        IPage<MsOrderItemView> page = new Page<MsOrderItemView>(pageDto.getPage(),pageDto.getPageSize());
        String deptIds="11,98";
        List<String> deptIdIn = Arrays.asList(deptIds.split(","));
        criteria.setSalesmanDeptIdIn(deptIdIn);
//        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysApprovalSettingLine.class, criteria);
//        predicate.orderByDesc("create_time");
//        baseMapper.selectPage(page, predicate);
//        TableList<MsOrderItemView> tableList=new TableList(page);
        getPage(pageDto);
        List<MsOrderItemView> orderList = orderItemList(criteria);
        PageInfo<MsOrderItemView> pageInfo = new PageInfo<>(orderList);
        TableList<MsOrderItemView> tableList=new TableList(pageInfo);
        return tableList;
    }

    @Override
    public List<MsOrderItemView> orderItemList(MsOrderItemViewQueryCriteria criteria) {
        return baseMapper.orderItemList(criteria);
    }

    @Override
    public String createLoginToken() {
        UserLogin userLogin=new UserLogin();
        //    userLogin.setPlatform(EmLoginPlatform.pc_3.getType());
        userLogin.setId("sjj-api");
        userLogin.setPassword("Kp@7Gm$2Rt%9Qw&5Jh#3Nz^8Lf*4Ds!6Xc$1Vb&7Mn");
        userLogin.setUserName("sjj-api");
        userLogin.setPlatform(2);
        YmUserDetails yhUserDetails = new YmUserDetails(userLogin);
        String s = JSONObject.toJSONString(userLogin);
        String token = jwtTokenUtil.generateToken(yhUserDetails);
        redisUtil.set(RedisKeyManager.pc_token_key+token, s);
        return tokenHead+token;
    }

    /**
     * 监听 领料出库动作
     */
    @Override
    public void listenerAddOutOrder(List<ListenerOutOrderItemDto> dtos) {
        String jsonString = JSONObject.toJSONString(dtos);
        log.info("---listenerAddOutOrder:"+jsonString);
        List<BigInteger> houseLocationOIDs=new ArrayList<>();
        for (ListenerOutOrderItemDto dto : dtos) {
            houseLocationOIDs.add(dto.getHouseLocationOID());
        }
        batchOnLight(houseLocationOIDs,"outOrder");


    }

    /**
     监听 选中的库存数据
     */
    @Override
    public void listenerClickStockItem(List<ListenerStockItemDto> items) {
        String jsonString = JSONObject.toJSONString(items);
        System.out.println("--listenerClickStockItem:"+jsonString);
        List<BigInteger> houseLocationOIDs=new ArrayList<>();
        for (ListenerStockItemDto dto : items) {
            houseLocationOIDs.add(dto.getHouseLocationOID());
        }
      //  batchOnLight(houseLocationOIDs,"stock");
    }


    public void batchOnLight(List<BigInteger> houseLocationOIDs,String source){
        IotTagQueryCriteria criteria=new IotTagQueryCriteria();
        criteria.setHouseLocationIdIn(houseLocationOIDs);
        List<IotTag> iotTags = iotTagService.queryAll(criteria);
        List<String> tagMacs=new ArrayList<>();
        for (IotTag iotTag : iotTags) {
            tagMacs.add(iotTag.getTagMac());
        }
        Gson gson = new Gson();
        String jsonStr = gson.toJson(tagMacs);

        //库位标签灯亮起
        JSONObject jsonObject = iotHttpUtil.httpPostStr("/iot/easyfinder/bgTag/light", jsonStr);
        JSONArray jsonArray1=null;
        if(jsonObject.getInteger("code")==200){
            jsonArray1 = jsonObject.getJSONArray("data");
        }


        //巷道灯亮
        List<IotTag> xiandaoDengs = iotTagService.findXiandaoDengByTagMacIn(tagMacs);
        List<String> xd_tagMacIn=new ArrayList<>();
        for (IotTag iotTag : xiandaoDengs) {
            xd_tagMacIn.add(iotTag.getTagMac());
        }
        Gson gson1 = new Gson();
        String jsonStr1 = gson1.toJson(xd_tagMacIn);
        JSONObject json_xd = iotHttpUtil.httpPostStr("/iot/easyfinder/bgTag/light", jsonStr1);
        JSONArray jsonArray2=null;
        if(json_xd.getInteger("code")==200){
            jsonArray2 = json_xd.getJSONArray("data");
        }
        //判断亮起的标签数量是否都一样
       int d=tagMacs.size()-jsonArray1.size();

    }

}
